package com.example.galerija;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.example.database.DatabaseHandler;
import com.example.database.RootProvider;
import com.example.database.Saldejums;


/**
 * A fragment representing a single Saldejums detail screen.
 * This fragment is either contained in a {@link SaldejumsListActivity}
 * in two-pane mode (on tablets) or a {@link SaldejumsDetailActivity}
 * on handsets.
 */
//public class SaldejumsDetailFragment extends Fragment {
public class SaldejumsDetailFragment extends ListFragment {
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";
    public static final String ARG_GROUP_ID = "group_id";
    /**
     * The Saldejums this fragment is presenting.
     */
    private Saldejums mItem;
    
    /**
     * The UI elements showing the details of the Saldejums
     */
    private TextView textCode;
    private TextView textItemCode;
    private TextView textDescription_lv;
    private TextView textDescription_eng;
    
    private static final String STATE_ACTIVATED_POSITION = "activated_position";
    /**
	 * The current activated item position. Only used on tablets.
	 */
	private int mActivatedPosition = ListView.INVALID_POSITION;
	    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public SaldejumsDetailFragment() {
    }
/*
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	
        super.onCreate(savedInstanceState);
        
        Log.v("ListFragment", "onCreate().");
        Log.v("ListsavedInstanceState", savedInstanceState == null ? "true" : "false");

        
       
        if  (getArguments().containsKey(ARG_GROUP_ID)) {
        	
        	mItem = DatabaseHandler.getInstance(getActivity()).getSaldejumsList(getArguments().getLong(ARG_GROUP_ID));
        	
        	
        }
        
      
       
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	
    	
      //  View rootView = inflater.inflate(R.layout.fragment_saldejums_detail, container, false);
    	 //View rootView = inflater.inflate(R.layout.fragment_saldejums_detail, container, false);
    	// GridView gridView = (GridView) rootView.findViewById(R.id.saldejumsgridview);
    	// gridView.setAdapter(new Saldejums(rootView.getContext()));// uses the view to get the context instead of getActivity()
       	View rootView = inflater.inflate(R.layout.fragment_saldejums_detail, container, false);
      
    	if (mItem != null) {
            textCode = ((TextView) rootView.findViewById(R.id.textSaldejumsCategoryCode));
            textCode.setText(mItem.group_code);
          //  textCode.setLayoutParams(new GridView.LayoutParams(85, 85));
            
            textItemCode = ((TextView) rootView.findViewById(R.id.textItemCode));
            textItemCode.setText(mItem.item_code);
          //  textItemCode.setLayoutParams(new GridView.LayoutParams(85, 85));
            
            textDescription_lv = ((TextView) rootView.findViewById(R.id.textSaldejumsDescriptionLv));
            textDescription_lv.setText(mItem.description_lv);
          //  textDescription_lv.setLayoutParams(new GridView.LayoutParams(85, 85));
            
        }
    	 
 
    	
    	 
        return rootView;
         
    }
    */
  
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);

		//
		/*
		  Log.w("ListFragment", "onCreate().");
	        Log.w("ListsavedInstanceState", savedInstanceState == null ? "true" : "false");

	    */    
	       /*
	        if  (getArguments().containsKey(ARG_GROUP_ID)) {
	        	
	        	
	        	mItem = DatabaseHandler.getInstance(getActivity()).getSaldejumsList(getArguments().getLong(ARG_GROUP_ID));
	        	
	        	Log.w("getArguments()", "containsKey(ARG_GROUP_ID)" +mItem.group_code);
	        }
	        */
		//
		
		setListAdapter(new SimpleCursorAdapter(getActivity(),
				R.layout.fragment_saldejums_listitem, null, new String[] {
			/*			
			Saldejums.COL_ITEM_CAT_CODE,Saldejums.COL_PRODUCT_GROUP_CODE, Saldejums.COL_ITEM_CODE, 
						Saldejums.COL_DESCRIPTION_LV,
						*/
			Saldejums.COL_PRODUCT_GROUP_CODE,Saldejums.COL_ITEM_CODE,Saldejums.COL_DESCRIPTION_LV },
						//Group.COL_PRODUCT_GROUP_CODE, Group.COL_DESCRIPTION_LV }, 
					//Show me group code
						//	new int[] {  R.id.cardIceGroupCode, R.id.cardGroupCodeDescription }, 0));
						//new int[] {  R.id.cardIceGroupCode, R.id.cardGroupCodeDescription }, 0));
						new int[] {  R.id.textSaldejumsCategoryCode, R.id.textItemCode, R.id.textSaldejumsDescriptionLv }, 0));
		
		//
		
				
		// Load the content
		//getLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>() {
		getLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>() {
			
			
			@Override
			public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			
				//String uri = RootProvider.URI_SALDEJUMS;
				String[] projection = { Saldejums.COL_ID,Saldejums.COL_ITEM_CAT_CODE,Saldejums.COL_PRODUCT_GROUP_CODE,
						  Saldejums.COL_ITEM_CODE,Saldejums.COL_DESCRIPTION_LV,Saldejums.COL_DESCRIPTION_ENG };
				String selection = "(("+ Saldejums.COL_PRODUCT_GROUP_CODE + " IS ?))"; //"1=1" or even "" works;  
			        mItem = DatabaseHandler.getInstance(getActivity()).getSaldejumsList(getArguments().getLong(ARG_GROUP_ID));
			    String[] selectionArgs = new String[] { String.valueOf(mItem.group_code) };
			    String sortOrder = Saldejums.COL_TOP_ITEM + " desc"; 
			        //String sortOrder = null;
			        return new CursorLoader(getActivity(), 
			        		RootProvider.URI_SALDEJUMS, 
			        		projection, 
			        		selection, 
			        		selectionArgs, 
			        		sortOrder);
						
			       
			}
			

			@Override
			public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
			
						((SimpleCursorAdapter) getListAdapter()).swapCursor(c);
					Log.w("Message", "onLoadFinished send ARG_GROUP_ID " +mItem.group_code);
				//
				}
			

			@Override
			public void onLoaderReset(Loader<Cursor> arg0) {
				((SimpleCursorAdapter) getListAdapter()).swapCursor(null);
				Log.w("onLoaderReset", "swapCursor(null)" +arg0);
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.w("Message", " onCreateView");
		return inflater.inflate(R.layout.fragment_saldejums_list, null);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		// Restore the previously serialized activated item position.
		if (savedInstanceState != null
				&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
			setActivatedPosition(savedInstanceState
					.getInt(STATE_ACTIVATED_POSITION));
		}
	}
    
	private void setActivatedPosition(int position) {
		if (position == ListView.INVALID_POSITION) {
			getListView().setItemChecked(mActivatedPosition, false);
		} else {
			getListView().setItemChecked(position, true);
		}

		mActivatedPosition = position;
	}
	
	/*
    @Override
    public void onPause() {
    	super.onPause();
    	updateSaldejumsFromUI();
    }
    
    private void updateSaldejumsFromUI() {
    	if (mItem != null) {
    		mItem.group_code = textCode.getText().toString();
    		mItem.description_lv = textDescription_lv.getText().toString();
    		
    		DatabaseHandler.getInstance(getActivity()).putSaldejums(mItem);
        }
        
    }
    */
}
